package cn.npe2.scaffolding.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.ArrayList;
import java.util.List;
import lombok.experimental.UtilityClass;

/**
 * 自定义分页工具类
 *
 * @author: Echan
 * @since: 2021-11-29 17:34:09
 */

@UtilityClass
public class PageUtil {

    /**
     * 针对list集合分页
     *
     * @param current 起始页
     * @param size    页面大小
     * @param list    集合数据
     * @param <T>     泛型类
     * @return
     */
    public <T> Page<T> page(Integer current, Integer size, List<T> list) {
        Page<T> page = new Page<>(current, size);
        int count = list.size();
        List<T> pageList = new ArrayList<>();
        int currId = current > 1 ? (current - 1) * size : 0;
        for (int i = 0; i < size && i < count - currId; i++) {
            pageList.add(list.get(currId + i));
        }
        page.setTotal(count);
        page.setPages(count % size == 0 ? count / size : count / size + 1);
        page.setRecords(pageList);
        return page;
    }

}
